home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / grafik / giflib11 / util / gifwedge.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-03  |  5.7 KB  |  168 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to create a test image of White and Red/Green/Blue levels for      *
  7. * test purposes. The Primary (RGB) and Secondary (YCM) are also displayed.   *
  8. * background.                                     *
  9. * Options:                                     *
  10. * -s Width Height : set image size.                         *
  11. * -l levels : number of color levels.                         *
  12. * -h : on line help.                                 *
  13. ******************************************************************************
  14. * History:                                     *
  15. * 4 Jan 90 - Version 1.0 by Gershon Elber.                     *
  16. *****************************************************************************/
  17.  
  18. #ifdef __MSDOS__
  19. #include <stdlib.h>
  20. #include <alloc.h>
  21. #endif /* __MSDOS__ */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include "gif_lib.h"
  27. #include "getarg.h"
  28.  
  29. #define PROGRAM_NAME    "GifWedge"
  30.  
  31. #define DEFAULT_WIDTH    640
  32. #define DEFAULT_HEIGHT    350
  33.  
  34. #define DEFAULT_NUM_LEVELS    16     /* Number of colors to gen the image. */
  35.  
  36. #ifdef __MSDOS__
  37. extern unsigned int
  38.     _stklen = 16384;                 /* Increase default stack size. */
  39. #endif /* __MSDOS__ */
  40.  
  41. #ifdef SYSV
  42. static char *VersionStr =
  43.         "Gif library module,\t\tGershon Elber\n\
  44.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  45. static char
  46.     *CtrlStr = "GifWedge l%-#Lvls!d s%-Width|Height!d!d h%-";
  47. #else
  48. static char
  49.     *VersionStr =
  50.     PROGRAM_NAME
  51.     GIF_LIB_VERSION
  52.     "    Gershon Elber,    "
  53.     __DATE__ ",   " __TIME__ "\n"
  54.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  55. static char
  56.     *CtrlStr =
  57.     PROGRAM_NAME
  58.     " l%-#Lvls!d s%-Width|Height!d!d h%-";
  59. #endif /* SYSV */
  60.  
  61. static int
  62.     NumLevels = DEFAULT_NUM_LEVELS,
  63.     ImageWidth = DEFAULT_WIDTH,
  64.     ImageHeight = DEFAULT_HEIGHT;
  65.  
  66. static void QuitGifError(GifFileType *GifFile);
  67.  
  68. /******************************************************************************
  69. * Interpret the command line and scan the given GIF file.              *
  70. ******************************************************************************/
  71. void main(int argc, char **argv)
  72. {
  73.     int    i, j, l, c, Error, LevelStep, LogNumLevels,
  74.     Count = 0, LevelsFlag = FALSE, SizeFlag = FALSE, HelpFlag = FALSE;
  75.     GifRowType Line;
  76.     GifColorType *ColorMap;
  77.     GifFileType *GifFile;
  78.  
  79.     if ((Error = GAGetArgs(argc, argv, CtrlStr,
  80.         &LevelsFlag, &NumLevels,
  81.         &SizeFlag, &ImageWidth, &ImageHeight,
  82.         &HelpFlag)) != FALSE) {
  83.     GAPrintErrMsg(Error);
  84.     GAPrintHowTo(CtrlStr);
  85.     exit(1);
  86.     }
  87.  
  88.     if (HelpFlag) {
  89.     fprintf(stderr, VersionStr);
  90.     GAPrintHowTo(CtrlStr);
  91.     exit(0);
  92.     }
  93.  
  94.     /* Make sure the number of levels is power of 2 (up to 32 levels.). */
  95.     for (i = 1; i < 6; i++) if (NumLevels == (1 << i)) break;
  96.     if (i == 6) GIF_EXIT("#Lvls (-l option) is not power of 2 up to 32.");
  97.     LogNumLevels = i + 3;               /* Multiple by 8 (see below). */
  98.     LevelStep = 256 / NumLevels;
  99.  
  100.     /* Make sure the image dimension is a multiple of NumLevels horizontally */
  101.     /* and 7 (White, Red, Green, Blue and Yellow Cyan Magenta) vertically.   */
  102.     ImageWidth = (ImageWidth / NumLevels) * NumLevels;
  103.     ImageHeight = (ImageHeight / 7) * 7;
  104.  
  105.     /* Open stdout for the output file: */
  106.     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
  107.     QuitGifError(GifFile);
  108.  
  109.     /* Dump out screen description with given size and generated color map:  */
  110.     /* The color map has 7 NumLevels colors for White, Red, Green and then   */
  111.     /* The secondary colors Yellow Cyan and magenta.                 */
  112.     if ((ColorMap = (GifColorType *)
  113.         malloc(8 * NumLevels * sizeof(GifColorType))) == NULL)
  114.     GIF_EXIT("Failed to allocate memory required, aborted.");
  115.  
  116.     for (i = 0; i < 8; i++)                   /* Set color map. */
  117.     for (j = 0; j < NumLevels; j++) {
  118.         l = LevelStep * j;
  119.         c = i * NumLevels + j;
  120.         ColorMap[c].Red = (i == 0 || i == 1 || i == 4 || i == 6) * l;
  121.         ColorMap[c].Green =    (i == 0 || i == 2 || i == 4 || i == 5) * l;
  122.         ColorMap[c].Blue = (i == 0 || i == 3 || i == 5 || i == 6) * l;
  123.     }
  124.  
  125.     if (EGifPutScreenDesc(GifFile,
  126.     ImageWidth, ImageHeight, LogNumLevels, 0, LogNumLevels, ColorMap)
  127.     == GIF_ERROR)
  128.     QuitGifError(GifFile);
  129.  
  130.     /* Dump out the image descriptor: */
  131.     if (EGifPutImageDesc(GifFile,
  132.     0, 0, ImageWidth, ImageHeight, FALSE, LogNumLevels, NULL) == GIF_ERROR)
  133.     QuitGifError(GifFile);
  134.  
  135.     fprintf(stderr, "\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
  136.             PROGRAM_NAME, GifFile -> ILeft, GifFile -> ITop,
  137.             GifFile -> IWidth, GifFile -> IHeight);
  138.  
  139.     /* Allocate one scan line to be used for all image.                 */
  140.     if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth)) == NULL)
  141.     GIF_EXIT("Failed to allocate memory required, aborted.");
  142.  
  143.     /* Dump the pixels: */
  144.     for (c = 0; c < 7; c++) {
  145.     for (i = 0, l = 0; i < NumLevels; i++)
  146.         for (j = 0; j < ImageWidth / NumLevels; j++)
  147.         Line[l++] = i + NumLevels * c;
  148.     for (i = 0; i < ImageHeight / 7; i++) {
  149.         if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
  150.         QuitGifError(GifFile);
  151.         fprintf(stderr, "\b\b\b\b%-4d", Count++);
  152.     }
  153.     }
  154.  
  155.     if (EGifCloseFile(GifFile) == GIF_ERROR)
  156.     QuitGifError(GifFile);
  157. }
  158.  
  159. /******************************************************************************
  160. * Close output file (if open), and exit.                      *
  161. ******************************************************************************/
  162. static void QuitGifError(GifFileType *GifFile)
  163. {
  164.     PrintGifError();
  165.     if (GifFile != NULL) DGifCloseFile(GifFile);
  166.     exit(1);
  167. }
  168.